﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using System.Text.RegularExpressions;

public partial class SkillifyMaster : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        txtSubscription.Attributes.Add("onFocus", "clearEmail(this)");
    }

    protected void btnInv_Click(object sender, EventArgs e)
    {
        if (IsValidEmailAddress(txtSubscription.Text))
        {
            DataTable myDt = GenericDAL.SetSubscription("", txtSubscription.Text);
            //MessageBox.Show("Welcome to SkilledRES Newsletter Subscription Members Zone", "Subscription Completed");
            if (myDt.Rows[0][0].ToString() == "SUCCESS")
            {
                txtSubscription.Text = "Subscription Successful";
                txtSubscription.ForeColor = Color.Green;
            }
            else
            {
                txtSubscription.Text = "Already Subscribed";
                txtSubscription.ForeColor = Color.Red;
            }
        }
        else
        {
            txtSubscription.Text = "Invalid Email Id";
            txtSubscription.ForeColor = Color.Red;
        }
    }

    public static bool IsValidEmailAddress(string sEmail)
    {
        if (sEmail == null)
        {
            return false;
        }

        int nFirstAT = sEmail.IndexOf('@');
        int nLastAT = sEmail.LastIndexOf('@');

        if ((nFirstAT > 0) && (nLastAT == nFirstAT) &&
        (nFirstAT < (sEmail.Length - 1)))
        {
            // address is ok regarding the single @ sign
            return (Regex.IsMatch(sEmail, @"(\w+)@(\w+)\.(\w+)"));
        }
        else
        {
            return false;
        }
    }
}
